package gs.android.bingomathfree;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.plus.PlusClient;
import com.google.android.gms.plus.PlusOneButton;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener {
	private static final int REQUEST_CODE_RESOLVE_ERR = 9000;
	private static final int PLUS_ONE_REQUEST_CODE = 0;

    private ConnectionResult result;
    private ProgressDialog dialog;
    private PlusOneButton plus;
    private PlusClient client;
	private Button play, settings, help, records;

	@Override
	public void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.main);
		
		play = (Button) findViewById(R.id.btnPlay);
		play.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), TaskActivity.class);
				startActivity(intent);
			}
		});
		
		settings = (Button) findViewById(R.id.btnSettings);
		settings.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), SettingsActivity.class);
				startActivity(intent);
			}
		});
		
		help = (Button) findViewById(R.id.btnHelp);
		help.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), InstructionActivity.class);
				startActivity(intent);
			}
		});
		
		records = (Button) findViewById(R.id.btnRecords);
		records.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), RecordsActivity.class);
				startActivity(intent);
			}
		});
	    
	    plus = (PlusOneButton) findViewById(R.id.plus_one_button);
		
		try {
			client = new PlusClient.Builder(this, this, this)
				.setActions("http://schemas.google.com/AddActivity", "http://schemas.google.com/BuyActivity")
	            .build();
			
		    dialog = new ProgressDialog(this);
		    dialog.setMessage("Signing in...");
		} catch (Exception e) { }
		
		if (!client.isConnected()) {
            if (result == null) {
                dialog.show();
            } else {
                try {
                	result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
                } catch (SendIntentException e) {
                    // Try connecting again.
                    result = null;
                    client.connect();
                }
            }
        }
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		plus.initialize("https://play.google.com/store/apps/details?id=" + getPackageName(), PLUS_ONE_REQUEST_CODE);
	}

    @Override
    protected void onStart() {
        super.onStart();
        client.connect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        client.disconnect();
    }

	@Override
	protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
		if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
			result = null;
	        client.connect();
	    }
	}

	@Override
	public void onConnectionFailed(ConnectionResult result) {
		if (dialog.isShowing()) {
            if (result.hasResolution()) {
            	try {
            		result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
            	} catch (SendIntentException e) {
            		client.connect();
            	}
            }
		}
		this.result = result;
	}

	@Override
	public void onConnected(Bundle connectionHint) {
		dialog.dismiss();
	}

	@Override
	public void onDisconnected() { }
}